﻿Namespace Web

    Friend Class Javascript

        Public Shared Function EncodeMensagem(ByVal str As String) As String
            Return String.Concat("decodeURIComponent('", str.Replace("""", "%22").Replace("'", "%22"), "')")
        End Function


        Public Shared Sub Executa(ByVal Script As String)
            Dim page As Page = HttpContext.Current.CurrentHandler
            ScriptManager.RegisterStartupScript(page, page.GetType, "script" & Guid.NewGuid.ToString, String.Concat("setTimeout(function (){ ", Script, "},0);"), True) '.Replace("""", "\"""),
        End Sub

#Region "Alert"

        Public Shared Sub Alert(ByVal Mensagem As String)
            Executa(String.Concat("alert(", EncodeMensagem(Mensagem), " );"))
        End Sub

        Public Shared Sub AlertRedirect(ByVal Mensagem As String, ByVal Url As String)
            Executa(String.Concat("alert(", EncodeMensagem(Mensagem), ");document.location = '", Url, "';"))
        End Sub

#End Region

#Region "PopUp"

        ''' <summary>
        ''' Width: 780
        ''' Height: 500
        ''' </summary>
        ''' <param name="Url"></param>
        ''' <remarks></remarks>
        Public Shared Sub PopUp(ByVal Url As String)
            PopUp(Url, 780, 500)
        End Sub

        Public Shared Sub PopUp(ByVal Url As String, ByVal Width As Integer, ByVal Height As Integer)
            Executa(String.Concat("open(""", Url, """, ""popup"", ""width=", Width, ", height=", Height, ", scrollbars=yes, top=0, left=0,scrolling=auto"");"))
        End Sub

#End Region

        Public Shared Sub ScriptBlock(ByVal Script As String)
            Dim page As Page = HttpContext.Current.CurrentHandler
            ScriptManager.RegisterClientScriptBlock(page, page.GetType, "script" & Guid.NewGuid.ToString, Script, True)
        End Sub

    End Class

End Namespace